package com.itydl.a05.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;

import com.itydl.a05.R;

/**
 * @author ydl
 * @version 仿美团第一阶段自定义View
 */

public class RefreshSecondStepView extends View {

    private Bitmap mEndBitmap;
    private int mWidth;//第二三状态的宽度
    private int mHeight;//第二三状态的高度

    public RefreshSecondStepView(Context context) {
        this(context,null);
    }

    public RefreshSecondStepView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        mEndBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pull_end_image_frame_05));
        mWidth = mEndBitmap.getWidth();
        mHeight = mEndBitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),measureWidth(widthMeasureSpec)*mHeight/mWidth);
    }

    /**
     * 获取控件的宽度，如果布局文件中为match_parent或者写死则大小就为这个值，如果是wrap_content,则测量第二张图片的宽度值设置给当前的控件宽度
     * @return
     * @param widthMeasureSpec
     */
    public int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        if(mode == MeasureSpec.EXACTLY){//精确地
            result = size;
        }else{
            result = mWidth;//
            if(mode == MeasureSpec.AT_MOST){//如果是wrap_content，取最小值
                result = Math.min(result,size);//取size和第二张图片的最小值
            }
        }
        return result;
    }
}
